'''
Created on Apr 3, 2010

@author: oabalbin
'''
import sys
import os
import glob
import MySQLdb;
from collections import deque,defaultdict
import signatures.db.tables as dt
import signatures.parsers.read_gene_lists as gpm

class gene_annotation():
    def __init__(self, dbhost, dbuser, dbpasswd, database):
        """ Database Information """
        self.myhost = dbhost
        self.myuser = dbuser
        self.mypasswd = dbpasswd
        self.mydb = database
        
        #self.tableName='entrez2hugo'
        self.tableName='entrez2hugo_long'
        'protA VARCHAR(40), protB VARCHAR(40), method VARCHAR(100), INDEX pA (protA), INDEX pB (protB)'
        
        self.tableDescription='tax_id VARCHAR(40), entrezID VARCHAR(40), hugoID VARCHAR(40), synonyms VARCHAR(40),'+\
                              ' description VARCHAR(200),seq_type VARCHAR(40), ensemble VARCHAR(200), locus VARCHAR(40),'+\
                              'long_desc VARCHAR(500), INDEX entrez (entrezID), INDEX hugo (hugoID)  '
        '''
        self.tableName='refflat2hugo'
        self.tableDescription='refflatID VARCHAR(40), hugoID VARCHAR(40), INDEX reflat (refflatID), INDEX hugo (hugoID)  '
        '''
        '''
        self.tableName='mouse2human'
        self.tableDescription='hugoMouseID VARCHAR(40), hugoHumanID VARCHAR(40), INDEX mouse (hugoMouseID), INDEX human (hugoHumanID)  '
        '''
        self.mt = dt.tables(dbhost, dbuser, dbpasswd, database)
        
    
    def create_connector(self):
        conn=MySQLdb.connect(host=self.myhost,user=self.myuser,passwd=self.mypasswd,db=self.mydb)
        return conn
    
    def close_connector(self, connector):
        connector.close
        
    def geneID2Entrez(self,inputfile,outfile):
        """
        It parses a file with information og entrez geneID, and HUGO information. 
        """
        
        header=False
        for line in inputfile:        
            line = line.strip('\n')
            fields = line.split('\t')
        
            # To skip headers, star reading samples in column 7 of the file
            if fields[0][0] == '#': 
                continue
            #print fields 
            
            tax_id, entrezGeneID, hugoGeneName, synonyms, description,seq_type = \
            fields[0], fields[1], fields[2], fields[4], fields[8], fields[9]  
            geneInfo = [tax_id, entrezGeneID, hugoGeneName, synonyms, description, seq_type]
            outfile.write(",".join(geneInfo).replace(',','\t')+'\n')
            
    def geneID2Entrez_long(self,inputfile,outfile):
        """
        It parses a file with information og entrez geneID, and HUGO information. 
        """
        
        header=False
        for line in inputfile:        
            line = line.strip('\n')
            fields = line.split('\t')
        
            # To skip headers, star reading samples in column 7 of the file
            if fields[0][0] == '#': 
                continue
            #print fields 
            
            tax_id, entrezGeneID, hugoGeneName, synonyms, description,seq_type, ensemble, locus, long_desc = \
            fields[0], fields[1], fields[2], fields[4], fields[8], fields[9], fields[5], fields[7], fields[13]

            geneInfo = [tax_id, entrezGeneID, hugoGeneName, synonyms, description, seq_type, ensemble, locus, long_desc]
            outfile.write(",".join(geneInfo).replace(',','\t')+'\n')


    
    def geneID2Reflat(self,inputfile,outfile):
        """
        Read a reflat hugo name file
        """
        
        for line in inputfile:        
            line = line.strip('\n')
            fields = line.split('\t')
        
            # To skip headers, star reading samples in column 7 of the file
            if fields[0][0] == '#': 
                continue
            #print fields 
            
            hugoGeneName, refflatName =  fields[0], fields[1]
            geneInfo = [refflatName, hugoGeneName]
            print geneInfo
            outfile.write(",".join(geneInfo).replace(',','\t')+'\n')
 
        
    def mouseID2Human(self,inputfile,outfile):
        """
        Read a mouse - human file
        A1BG      1    A1bg      MGI:2152878
        """
        
        for line in inputfile:        
            line = line.strip('\n')
            fields = line.split('\t')
        
            # To skip headers, star reading samples in column 7 of the file
            if fields[0][0] == '#':
                continue
            #print fields 
            
            hugoHumanName, hugoMouseName =  fields[0], fields[2]
            geneInfo = [hugoMouseName, hugoHumanName]
            print geneInfo
            outfile.write(",".join(geneInfo).replace(',','\t')+'\n')

        
    def create_table(self,folderpath):
        
        self.mt.create_connector()
        dbfiles=[]
        for infile in glob.glob( os.path.join(folderpath, '*.db') ):
            dbfiles.append(infile)

        for dbf in dbfiles:     
            if not(self.mt.table_existence(self.tableName)):
                self.mt.create_table(self.tableName,self.tableDescription)
                
            self.mt.load_data_table(self.tableName,dbf)
    
    #### make queries     
    def translate_hugo2entrez(self,genelist):
        """
        translate a genelist from hugo ID to entrez ID 
        """        
        genes = []
        conn = self.create_connector()
        cursor=conn.cursor()
        
        for gen in genelist:
            query='select entrezID from entrez2hugo where hugoID=\"'+gen+'\";'
            cursor.execute(query)
            results = cursor.fetchall()
            if len(results)==0:
                print gen
            else:
                #print gen,results[0][0]
                genes.append(results[0][0])
        
        return genes
    
    
    def translate_entrez2hugo(self,genelist):
        """
        translate a genelist from hugo ID to entrez ID 
        """
        genes = []
        conn=self.create_connector()
        cursor=conn.cursor()
        
        for gen in genelist:
            query='select hugoID from entrez2hugo where entrezID=\"'+gen+'\";'
            cursor.execute(query)
            results = cursor.fetchall()
            #print results[0][0]
            genes.append(results[0][0])
        
        return genes
    
    def translate_hugo2refflat(self,genelist):
        """
        translate a genelist from hugo ID to reflat ID 
        """
        genes = deque()
        conn=self.create_connector()
        cursor=conn.cursor()
        
        for gen in genelist:
            query='select refflatID from refflat2hugo where hugoID=\"'+gen+'\";'
            cursor.execute(query)
            results = cursor.fetchall()
            
            if len(results)==0:
                print gen
            else:
                #print gen,results[0][0]
                genes.append(results[0][0])
        
        return genes

    def translate_refflat2hugo(self, genelist):
        """
        translate a genelist from reflat ID to hugo ID 
        """
        genes = deque()
        conn=self.create_connector()
        cursor=conn.cursor()
        
        for gen in genelist:
            query='select hugoID from refflat2hugo where refflatID=\"'+gen+'\";'
            cursor.execute(query)
            results = cursor.fetchall()
            
            if len(results)==0:
                print gen
            else:
                #print gen,results[0][0]
                genes.append(results[0][0])
        
        return genes
    
    def translate_mouse2human(self, genelist):
        """
        translate from hugoMouseID to hugoHumanID
        """
        genes = deque()
        conn=self.create_connector()
        cursor=conn.cursor()
        notfound=[]
        for gen in genelist:
            query='select hugoHumanID from mouse2human where hugoMouseID=\"'+gen+'\";'
            cursor.execute(query)
            results = cursor.fetchall()
            
            if len(results)==0:
                notfound.append(gen)
            else:
                #print gen,results[0][0]
                genes.append(results[0][0])
        
        return genes

        
    def translate_human2mouse(self, genelist):
        """
        translate from hugoHumanID to hugoMouseID
        """
        genes = deque()
        conn=self.create_connector()
        cursor=conn.cursor()
        
        for gen in genelist:
            query='select hugoMouseID from mouse2human where hugoHumanID=\"'+gen+'\";'
            cursor.execute(query)
            results = cursor.fetchall()
            
            if len(results)==0:
                print gen
            else:
                #print gen,results[0][0]
                genes.append(results[0][0])
        
        return genes



    def convert_tusfile(self,inputfile,ouputfile):
        """
        It converts a file with info tag - list of genes in refflat format
        to a file with tag - genes in hugo format.
        It also returns the new dictionary
        """
        
        self.create_connector()
        tu_genes = defaultdict(list)

        i=0
        for line in inputfile:
            line = line.strip('\n')
            fields = line.split('\t')
            
            tu=fields[0]
            genes=fields[1:]
            hugolist = list(set(self.translate_refflat2hugo(genes)))
            #print tu,len(genes),len(hugolist)  
            tu_genes[tu].append(hugolist) 
            
            if i== 0:
                newlist = set(hugolist)
                i=1
            else:
                newlist.intersection_update(hugolist)
                #print tu, newlist
            ouputfile.write(tu+'\t'+",".join(hugolist).replace(",","\t")+'\n')
        
        return tu_genes
    
    

    def get_gene_long_description(self,genelist, outputfile):
        """
        """
        conn=self.create_connector()
        cursor=conn.cursor()
        
        for gen in genelist:
            query='select long_desc from entrez2hugo_long where hugoID =\"'+gen+'\";'
            cursor.execute(query)
            results = cursor.fetchall()

            if len(results)==0:
                outputfile.write(gen+'\t'+"No description found"+'\n')
            else:
                outputfile.write(gen+'\t'+results[0][0]+'\n')

        
        


#gp = gpm.geneparser()

#inputfile=open('/home/oabalbin/downloads/Homo_sapiens.gene_info')
#ouputfile=open('/home/oabalbin/projects/gene_nomenclature/entrez2hugo_long.db','w')
#folderpath='/home/oabalbin/projects/gene_nomenclature/'
#genelist_file = '/data/projects/outliers/2010_06_26_12_53_breast_outfile_outliers_list_names.txt'
'''
genelist_file = '/data/projects/outliers/HCC1599_list'
genelist = genelist = gp.list_of_names(open(genelist_file))
outputfile=genelist_file+'_description'

ga = gene_annotation("localhost", "oabalbin", "oscar", "gene_annotation")

#ga.create_connector()
#ga.geneID2Entrez_long(inputfile,ouputfile)
#ga.create_table(folderpath)

ga.get_gene_long_description(genelist, open(outputfile,'w'))
'''

'''
genelist1=gp.list_of_names(open('/home/oabalbin/projects/erg_regulation/100_ERG_chip'))
genelist2=gp.list_of_names(open('/home/oabalbin/projects/erg_regulation/ERG_regulation_Entrez'))
folderpath='/home/oabalbin/projects/gene_nomenclature/'

'''



'''
#sys.exit(0)
ga = gene_annotation("localhost", "oabalbin", "oscar", "gene_annotation")
gp = gpm.geneparser()

#genelist=gp.list_of_names(open('/home/oabalbin/projects/networks/EZH2_lists/allCodingGenes.txt'))
genelist=gp.list_of_names(open('/home/oabalbin/projects/networks/Kinases_lists/output/prostate_outliers_genelist_normals.txt'))
#inputfile=open('/home/oabalbin/projects/brusha_analysis/HMD_HumanPhenotype.rpt')
ouputfile=open('/home/oabalbin/projects/networks/Kinases_lists/output/prostate_outliers_genelist_hugo_normals.txt','w')


#genelist = ['GSTP1','S100A6','ANXA2','GSTM1','GSTM4','LAMA3','GPX3','SERPINB1','DSC3','VIM','RARRES1','PDLIM4']
#genelist = ['GSTP1']
print len(genelist)
#newgenelist = list(set(ga.translate_refflat2hugo(genelist)))
newgenelist = ga.translate_refflat2hugo(genelist)
print len(newgenelist)
for i in newgenelist:
    ouputfile.write(i+"\n")
'''

'''
######### TUs conversion file from NM to Hugo names
inputfile=open('/home/oabalbin/projects/networks/EZH2_lists/output/2010_05_17_17_01/sdv/2010_05_17_17_01_correlated_pairs.txt')
ouputfile=open('/home/oabalbin/projects/networks/EZH2_lists/output/2010_05_17_17_01/sdv/2010_05_17_17_01_correlated_pairs_hugonames.txt','w')

inputfile2=open('/home/oabalbin/projects/networks/EZH2_lists/output/2010_05_22_22_02/sdv/2010_05_22_22_02_correlated_pairs.txt')
ouputfile2=open('/home/oabalbin/projects/networks/EZH2_lists/output/2010_05_22_22_02/sdv/2010_05_22_22_02_correlated_pairs_hugonames.txt','w')
ouputfile3=open('/home/oabalbin/projects/networks/EZH2_lists/output/2010_05_22_22_02/sdv/2010_05_22_22_02_correlated_pairs_intersection.txt','w')


tugenes_allsamples = ga.convert_tusfile(inputfile,ouputfile)
tugenes_notmets = ga.convert_tusfile(inputfile2,ouputfile2)

allsamplestus = set(tugenes_allsamples.keys())
notmetstus = set(tugenes_notmets.keys())

commontus = allsamplestus.intersection(notmetstus)

print allsamplestus
print notmetstus
print commontus

commontugenes=defaultdict(list)
for tu in commontus:
    
    genes1,genes2=set(tugenes_allsamples[tu][0]),set(tugenes_notmets[tu][0])
    commongenes = list(genes1.intersection(genes2))
    commontugenes[tu].append(commongenes)
    print tu, commongenes
    ouputfile3.write(tu+'\t'+",".join(commongenes).replace(",","\t")+'\n')
    

'''
'''
i=0
for line in inputfile:
    line = line.strip('\n')
    fields = line.split('\t')
    
    tu=fields[0]
    genes=fields[2:]
    hugolist = list(set(ga.translate_refflat2hugo(genes)))
    if i== 0:
        newlist = set(hugolist)
        i=1
    else:
        newlist.intersection_update(hugolist)
        print tu, newlist
    ouputfile.write(tu+'\t'+",".join(hugolist).replace(",","\t")+'\n')


ouputfile2.write(",".join(newlist).replace(",","\n"))
'''



#ga.mouseID2Human(inputfile,ouputfile)
#ga.create_table(folderpath)
#folderpath='/home/oabalbin/projects/brusha_analysis/'
#genelist = ga.translate_hugo2refflat(genelist)
#print genelist
#ga.translate_hugo2entrez(genelist1)
#ga.translate_entrez2hugo(genelist2)



